package com.wenyu.wenyuyuedu.activity.personage;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.global.MyApplication;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.DataCleanManager;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;

import static com.wenyu.wenyuyuedu.R.id.btn_quiet_login;

/*
* 设置
* */
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.rl_help_setting)
    RelativeLayout rlHelpSetting;
    @InjectView(R.id.iv_left)
    ImageView ivLeft;
    @InjectView(R.id.tv_sdk)
    TextView tvSdk;
    @InjectView(R.id.rl_clearmemory)
    RelativeLayout rlClearmemory;
    @InjectView(btn_quiet_login)
    Button btnQuietLogin;
    @InjectView(R.id.rl_garde_setting)
    RelativeLayout rlGardeSetting;
    @InjectView(R.id.rl_regard_setting)
    RelativeLayout rlRegardSetting;
    private Intent intent;
    private int is_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        StatusBarUtil.setStatusBar(SettingActivity.this, R.color.white);
        ButterKnife.inject(this);
        initView();
    }
    /*
    * 初始化控件的点击事件
    * */
    private void initView() {
        intent = new Intent();
        //开启获取本应用缓存的子线程
        new GetCacheThread().start();
        rlHelpSetting.setOnClickListener(this);
        ivLeft.setOnClickListener(this);
        btnQuietLogin.setOnClickListener(this);
        rlClearmemory.setOnClickListener(this);
        rlGardeSetting.setOnClickListener(this);
        rlRegardSetting.setOnClickListener(this);
        String token = AbSharedUtil.getString(SettingActivity.this, SharedConstants.USER_TOKEN);
        if(token == null || token.equals("")){
            btnQuietLogin.setText("登录");
            is_login = 0;
        }else {
            btnQuietLogin.setText("退出登录");
            is_login = 1;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.iv_left:
                finish();
                break;
            //常见问题
            case R.id.rl_help_setting:
                intent = new Intent(SettingActivity.this, CommonIssunActivity.class);
                startActivity(intent);
                break;
            //清除缓存
            case R.id.rl_clearmemory:
                new CleanCacheThread().start();
                break;
            /*退出登录*/
            case btn_quiet_login:
                if(is_login ==0){
                    intent.setClass(SettingActivity.this,LoginActivity.class);
                    startActivity(intent);
                }else if(is_login == 1){
                    showQuiet();
                }
                break;
            /*去评分*/
            case R.id.rl_garde_setting:
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            /*关于*/
            case R.id.rl_regard_setting:
                Intent intentRegard = new Intent(SettingActivity.this, RegardActivity.class);
                startActivity(intentRegard);
                break;
        }
    }

    AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示提示用户退出的dialog*/
    private void showQuiet() {
        ad = new AlertDialog.Builder(SettingActivity.this).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.quit_login_dailog);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Platform platform = ShareSDK.getPlatform(Wechat.NAME);
                platform.removeAccount(true);
                AbSharedUtil.putString(SettingActivity.this, SharedConstants.USER_TOKEN, "");
                AbSharedUtil.putString(SettingActivity.this, SharedConstants.USER_NICKNAME, null);
                AbSharedUtil.putString(SettingActivity.this, SharedConstants.USER_ICON, null);
                intent.setClass(SettingActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });
    }


    /**
     * 获取缓存的子线程
     */
    private class GetCacheThread extends Thread {
        @Override
        public void run() {
            File externalCacheDir = MyApplication.mApp.getExternalCacheDir();
            File cacheDir = MyApplication.mApp.getCacheDir();
            final long size = DataCleanManager.getFolderSize(externalCacheDir) + DataCleanManager.getFolderSize(cacheDir);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvSdk.setText(DataCleanManager.getFormatSize(size / 10));
                }
            });
        }
    }

    /**
     * 清理缓存的子线程
     */
    private class CleanCacheThread extends Thread {
        @Override
        public void run() {
            DataCleanManager.cleanInternalCache(MyApplication.mApp);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvSdk.setText("0KB");
                    MyToast.showToast(SettingActivity.this, "清理完成!", Toast.LENGTH_SHORT);
                }
            });
        }
    }
}
